#include <allegro.h>


BITMAP *buffer ,*bonus,*background,*cart ,*back_ground,*backgroundcol,*left[9] ,*right[9] ,*eleft[9] ,*eright[9]  ,*jumpr , *jumpl, *obs ,*vie[11];

void Affichage();
int temps =0,te=1;
float dx=1, dy=0 ,bh=0,cx=0,cy=0 ,cxnxx=0,cxnxxx=0;
int in, mar=1 ,sur,by=-100,carr=0 ,pos=1,pos2=-1,i=0 ,j=0,f=0,h=0,pourvie=10,boad=0,nbdevie=2,jj=0,evie=3;
int bb1 =1;
int get_pixel(BITMAP* Buffer,int x,int y);
typedef struct {int en_vie ;
                 float x ;
                 float y ;
                }bon ;
bon b1 ;
typedef struct { float ix;
                 float x ;
                 float y ;
                }perso ;
perso p ;
typedef struct {int en_vie ;
                float x ;
                float y ;
                }ennem  ;
ennem e ;


int collision_background_ennem_right(ennem e,BITMAP* backgroundcol);
int collision_background_ennem_left(ennem e,BITMAP* backgroundcol);
int collisionbonus (perso p, bon b1);
int collisioncart(perso p, float cx , float cy);
int collision(perso p, ennem e);

int collision_background_left(perso p,BITMAP *backgroundcol);
int collision_background_right(perso p,BITMAP *backgroundcol);

int collision_background_down(perso p,BITMAP* backgroundcol);
int collision_background_up(perso p,BITMAP* backgroundcol);

int collision_perso_ennem_up(perso p,ennem e);
int collision_perso_ennem_left(perso p,ennem e);
int collision_perso_ennem_down(perso p,ennem e);
int collision_perso_ennem_right(perso p,ennem e);

int main ()
{

	allegro_init();
	install_keyboard();
	set_color_depth(16);
	set_gfx_mode(GFX_AUTODETECT_WINDOWED,1300,700,0,0);
	buffer=create_bitmap(7996,700);
	background=load_bitmap("background.bmp",0);
	//back_ground=load_bitmap("back-ground.bmp",0);
	backgroundcol=load_bitmap("background_collision.bmp",0);
	bonus=load_bitmap("bonus.bmp",0);
	eleft[0]=load_bitmap("eleft0.bmp",0);
    eleft[1]=load_bitmap("eleft0.bmp",0);
    eleft[2]=load_bitmap("eleft0.bmp",0);
	eleft[3]=load_bitmap("eleft1.bmp",0);
	eleft[4]=load_bitmap("eleft1.bmp",0);
	eleft[5]=load_bitmap("eleft1.bmp",0);
	eleft[6]=load_bitmap("eleft2.bmp",0);
	eleft[7]=load_bitmap("eleft2.bmp",0);
	eleft[8]=load_bitmap("eleft2.bmp",0);
	eright[0]=load_bitmap("eright0.bmp",0);
	eright[1]=load_bitmap("eright0.bmp",0);
	eright[2]=load_bitmap("eright0.bmp",0);
	eright[3]=load_bitmap("eright1.bmp",0);
	eright[4]=load_bitmap("eright1.bmp",0);
	eright[5]=load_bitmap("eright1.bmp",0);
	eright[6]=load_bitmap("eright2.bmp",0);
	eright[7]=load_bitmap("eright2.bmp",0);
	eright[8]=load_bitmap("eright2.bmp",0);
	vie[0]=load_bitmap("vie0.bmp",0) ;vie[1]=load_bitmap("vie1.bmp",0) ;vie[2]=load_bitmap("vie2.bmp",0) ;vie[3]=load_bitmap("vie3.bmp",0) ;vie[4]=load_bitmap("vie4.bmp",0) ;vie[5]=load_bitmap("vie5.bmp",0) ;vie[6]=load_bitmap("vie6.bmp",0) ;vie[7]=load_bitmap("vie7.bmp",0) ;vie[8]=load_bitmap("vie8.bmp",0) ;vie[9]=load_bitmap("vie9.bmp",0) ; vie[10]=load_bitmap("vie10.bmp",0);
    cart=load_bitmap("cart.bmp",0);
	left[0]=load_bitmap("left0.bmp",0);
	left[1]=load_bitmap("left0.bmp",0);
	left[2]=load_bitmap("left0.bmp",0);
	left[3]=load_bitmap("left1.bmp",0);
	left[4]=load_bitmap("left1.bmp",0);
	left[5]=load_bitmap("left1.bmp",0);
	left[6]=load_bitmap("left2.bmp",0);
	left[7]=load_bitmap("left2.bmp",0);
	left[8]=load_bitmap("left2.bmp",0);

	right[0]=load_bitmap("right0.bmp",0);
	right[1]=load_bitmap("right0.bmp",0);
	right[2]=load_bitmap("right0.bmp",0);
	right[3]=load_bitmap("right1.bmp",0);
	right[4]=load_bitmap("right1.bmp",0);
	right[5]=load_bitmap("right1.bmp",0);
	right[6]=load_bitmap("right2.bmp",0);
	right[7]=load_bitmap("right2.bmp",0);
	right[8]=load_bitmap("right2.bmp",0);
	jumpr=load_bitmap("jumpr.bmp",0);
	jumpl=load_bitmap("jumpl.bmp",0);
    obs=load_bitmap("obs.bmp",0) ;
	p.x= 200;
	p.ix= 200;
	p.y=200 ;
	e.x=10000 ;
	e.y=550 ;
	e.en_vie=0;
	by=100 ;
	bh= 0;
	b1.x=10000;
	b1.y=200;
	b1.en_vie=0;
    mar=1 ,sur=0;
	while (!key[KEY_ESC])
 	{sur = 0 ;
 	if (   collisionbonus ( p,b1)  )
    {   pourvie++;
        b1.en_vie=0;
        b1.x=60000;
    }

    if ( p.x < e.x && p.x > e.x-500 && carr==0 &&te==1 )
    {
        cxnxx=-1; cx=e.x ; cy=e.y+50;carr=1;te=0;
    }
     if ( p.x > e.x && p.x < e.x+500 && carr==0 && te ==1 )
    {
      cxnxx=1;cx=e.x ; cy=e.y+50; carr=1;te=0;
    }


    temps++;
    if ( temps > 20 ) { temps = 0 ;te=1;}
    if (collisioncart( p,  cx , cy)   )
    {
      cx=0;cy=0;  cxnxx=0 ; cxnxxx=0; carr=0; pourvie -- ;te=0;
    }
    if ( cxnxxx>=500  ){ cxnxx=0;cxnxxx=0; carr=0; }
	if ( p.x < e.x-100 || p.x> e.x+100 )
        {
            boad=0;
        }
	    if ( collision( p,  e)&& boad==0 && key[KEY_D])
        {
          evie --;
          boad = 1 ;


        }

         if ( evie ==0){ e.x=e.x+10000; evie=3 ;e.en_vie=0; }
       if ( !key[KEY_RIGHT]&&!key[KEY_LEFT] ){
                 if ( p.x >e.x && !collision_perso_ennem_left(p,e)&& !collision_background_ennem_right( e, backgroundcol)   )
      {   h++ ;
          pos2=1;
          e.x=e.x+0.5 ;

      }
      if ( p.x <e.x-80 && !collision_perso_ennem_right(p,e) && !collision_background_ennem_left(e, backgroundcol)  )
      {   f++;
      pos2=-1;
        e.x=e.x-0.5 ;
      }}

       /* *********************************************************************** */
        if (p.ix<600)	{

	 if (key[KEY_LEFT]&& (!collision_background_left(p,backgroundcol)))
                       {

                            p.x=p.x-2 ;
                            j++;
                            p.ix=p.ix-2 ;
                             pos = -1;
                       }
                            if (key[KEY_RIGHT]&& (!collision_background_right(p,backgroundcol)))
                        {
                           p.x=p.x+2 ;
                            i++;
                            p.ix=p.ix+2 ;
                             pos = 1;
                        }
       if(!collision_background_down(p,backgroundcol)) { p.y=p.y+10; by = by-3 ;  usleep (10000); }
		if (mar==1)
		{
			Affichage();
		}   if (sur == 0 )
                            {

                            if (key[KEY_SPACE] )
                           {
                              do {
                                                           dx = dx*1.07 ;
                                if (key[KEY_LEFT]  && (!collision_background_up(p,backgroundcol))&& (!collision_background_left(p,backgroundcol)))
                           {
                            jj=1 ;
                            p.x = p.x - (dx*2) ;
                            p.ix = p.ix - (dx*2) ;

                            }
                            if (key[KEY_RIGHT]  && (!collision_background_up(p,backgroundcol))&& (!collision_background_right(p,backgroundcol)))
                             {
                                 jj=2;
                             p.x = p.x + (dx*2) ;
                             p.ix = p.ix + (dx*2) ;
                             }
                                                           by = by-3 ;
                                                           p.y= p.y-10;
                                                           dy= dy+10 ;
                                                           Affichage();
                                                          usleep (10000);

                                    } while (key[KEY_SPACE] && (dy < 201) && !collision_background_up(p,backgroundcol)&& !collision_background_right(p,backgroundcol)&& (!collision_background_left(p,backgroundcol)) );

                            sur =1 ;
                           }
                            }
            if (sur == 1)
   {

                          do
                      {
                                        dx= dx/1.07 ;
                                        if (key[KEY_LEFT]&&(!collision_background_left(p,backgroundcol)))
                           {

                            p.x = p.x - (dx*2) ;
                            p.ix = p.ix - (dx*2) ;
                            e.x++;
                            }
                            if (key[KEY_RIGHT]&&(!collision_background_right(p,backgroundcol)))
                             {
                             p.x = p.x + (dx*2) ;
                             p.ix = p.ix + (dx*2) ;
                             e.x--;
                             }

                                        by=by+3;
                                        p.y=p.y+10;

                                        Affichage();
                                        usleep (10000);
                    } while  (  !collision_background_down(p,backgroundcol)) ;
                    jj =0 ;
   }

                     dy =0 ;
                     dx =1 ;
         }
         /* *********************************************************************** */

    if (p.ix>=100)
    {
        if ( p.ix >1000 && p.ix<1000 && b1.en_vie==0   ) { b1.en_vie=1 ; b1.x=1300; }
        if ( p.ix >1500 && p.ix<1710 && b1.en_vie==0 ) { b1.en_vie=1 ;b1.x=1300;}
        if ( p.ix >2400 && p.ix<2410 && b1.en_vie==0 ) { b1.en_vie=1 ;b1.x=1300;}
        if ( p.ix >3000 && p.ix<3110 && b1.en_vie==0 ) { b1.en_vie=1 ;b1.x=1300;}
        if ( p.ix >3600 && p.ix<3610 && b1.en_vie==0 ) { b1.en_vie=1 ;b1.x=1300;}
      //if ( p.ix >710 && p.ix<710 && e.en_vie==0 ) { e.en_vie=1 ;e.x=1300;}
        if ( p.ix >1500 && p.ix<1510&& e.en_vie==0   ) { e.en_vie=1 ; e.x=1300; }
        if ( p.ix >2400 && p.ix<2410 && e.en_vie==0 ) { e.en_vie=1 ;e.x=1300;e.y=400;}
     // if ( p.ix >3000 && p.ix<3110 && e.en_vie==0 ) { e.en_vie=1 ;e.x=1300;}
      //if ( p.ix >3600 && p.ix<3610 && e.en_vie==0 ) { e.en_vie=1 ;e.x=1300;}
         if (key[KEY_LEFT]&& (!collision_background_left(p,backgroundcol)&& !collision_perso_ennem_left(p,e) ))
                            { if ( p.x >e.x+80 && !collision_background_ennem_right( e, backgroundcol) ){   h++ ;pos2=1;e.x=e.x+0.5 ;}if ( p.x <e.x-80 && !collision_background_ennem_left( e,backgroundcol)){   f++;pos2=-1;e.x=e.x-0.5 ;}

                            pos = -1;
                            b1.x=b1.x+2;
                            cx=cx+2;
                            e.x=e.x+2;
                            bh=bh+2 ;
                            p.ix=p.ix-2 ;
                             j++;

                            }
                            if (key[KEY_RIGHT]&& (!collision_background_right(p,backgroundcol))&& !collision_perso_ennem_right(p,e) )
                             { if ( p.x >e.x+80 &&!collision_background_ennem_right( e, backgroundcol)){   h++ ;pos2=1;e.x=e.x+0.5 ;}if ( p.x <e.x-80&&!collision_background_ennem_left(e, backgroundcol) ){   f++;pos2=-1;e.x=e.x-0.5 ;}
                             b1.x=b1.x-2;
                            cx=cx-2;
                            e.x=e.x-2;
                            pos = 1;
                            bh=bh-2 ;
                            p.ix=p.ix+2 ;

                            i++ ;}
       if(!collision_background_down(p,backgroundcol)&&!collision_perso_ennem_down( p, e)) { p.y=p.y+10; by = by-3 ;  usleep (10000); }
		if (mar==1)
		{
			Affichage();
		}   if (sur == 0 )
                            {

                            if (key[KEY_SPACE] )
                           {
                              do { if (   collisionbonus ( p,b1)  ){   pourvie++;b1.en_vie=0;b1.x=6000;}

                                                           dx = dx*1.07 ;
                                if (key[KEY_LEFT]  && (!collision_background_up(p,backgroundcol)&& !collision_perso_ennem_left(p,e)  )&& (!collision_background_left(p,backgroundcol))&&!collision_perso_ennem_down( p, e) )
                           {           if ( p.x >e.x+80&& !collision_background_ennem_right( e, backgroundcol)  ){   h++ ;pos2=1;e.x=e.x+0.5 ;}if ( p.x <e.x-80&& !collision_background_ennem_left( e, backgroundcol)  ){   f++;pos2=-1;e.x=e.x-0.5 ;}
                           jj =1 ;
                            b1.x=b1.x+(dx*2);
                           cx=cx+(dx*2);
                           e.x=e.x+(dx*2);
                           bh= bh + (dx*2) ;
                            p.ix = p.ix - (dx*2) ;

                            }
                            if (key[KEY_RIGHT]  && (!collision_perso_ennem_right(p,e) && (!collision_background_right(p,backgroundcol)))&&!collision_perso_ennem_down( p, e))
                             { if ( p.x >e.x+80&& !collision_background_ennem_right(e, backgroundcol) ){   h++ ;pos2=1;e.x=e.x+0.5 ;} if ( p.x <e.x-80&& !collision_background_ennem_left( e, backgroundcol)  ){   f++;pos2=-1;e.x=e.x-0.5 ;}
                                 e.x=e.x-(dx*2);
                                 b1.x=b1.x-(dx*2);
                                 cx=cx-(dx*2);
                             bh = bh - (dx*2) ;
                             p.ix = p.ix + (dx*2) ;

                             jj =2 ;
                             }
                                                           by = by-3 ;
                                                           p.y= p.y-10;
                                                           dy= dy+10 ;
                                                           Affichage();
                                                          usleep (10000);

                                    } while (key[KEY_SPACE] && (dy < 201)&& !collision_background_right(p,backgroundcol)&& (!collision_background_left(p,backgroundcol))&&!collision_perso_ennem_down( p, e) );

                            sur =1 ;
                           }
                            }
            if (sur == 1)
   {

                          do
                      { if (   collisionbonus ( p,b1)  ){   pourvie++;b1.en_vie=0;b1.x=6000;    }

                                        dx= dx/1.07 ;
                                        if (key[KEY_LEFT]&&(!collision_background_left(p,backgroundcol)&& !collision_perso_ennem_left(p,e) &&!collision_perso_ennem_down( p, e)))
                           {
                             if ( p.x >e.x && !collision_background_ennem_right( e, backgroundcol)  ){   h++ ;pos2=1;e.x=e.x+0.5 ;}if ( p.x <e.x-80 && !collision_background_ennem_left(e, backgroundcol) ){   f++;pos2=-1;e.x=e.x-0.5 ;}
                           b1.x=b1.x+(dx*2);
                           cx=cx+(dx*2);
                           e.x=e.x+(dx*2);
                           bh = bh + (dx*2) ;
                            p.ix = p.ix - (dx*2) ;

                            }
                            if (key[KEY_RIGHT]&&(!collision_background_right(p,backgroundcol)&& !collision_perso_ennem_right(p,e))&&!collision_perso_ennem_down( p, e))
                             {
                              if ( p.x >e.x&& !collision_background_ennem_right(e, backgroundcol)  ){   h++ ;pos2=1;e.x=e.x+0.5 ;}if ( p.x <e.x-80&& !collision_background_ennem_left(e, backgroundcol)  ){   f++;pos2=-1;e.x=e.x-0.5;}
                             b1.x=b1.x-(dx*2);
                             cx=cx- (dx*2);
                              e.x=e.x-(dx*2);
                             bh = bh - (dx*2) ;
                             p.ix = p.ix + (dx*2) ;

                             }

                                        by=by+3;
                                        p.y=p.y+10;

                                        Affichage();
                                        usleep (10000);
                    } while  (  !collision_background_down(p,backgroundcol)&&!collision_perso_ennem_down( p, e)) ;
                    jj =0 ;

   }

                     dy =0 ;
                     dx =1 ;

    }
   }
    destroy_bitmap(left[3] );
	destroy_bitmap(right[3]);
	destroy_bitmap(eleft[3] );
	destroy_bitmap(eright[3]);
    destroy_bitmap(back_ground);
    destroy_bitmap(background);

	destroy_bitmap(jumpr);
	destroy_bitmap(jumpl);
	return 0;
}END_OF_MAIN();

void Affichage()
{
	clear(buffer);
	//draw_sprite(buffer,back_ground,0,by-110);
    draw_sprite(buffer,background,bh,0);
      if ( jj ==2 ) {draw_sprite(buffer,jumpr  ,p.x,p.y );  }
    else
     if ( jj ==1 ) {draw_sprite(buffer,jumpl  ,p.x,p.y );  }
     else
        if (  pos ==1  )
    {
	draw_sprite(buffer,right[i%9]  ,p.x,p.y );
	}else
	if ( pos ==-1 )
    {
	draw_sprite(buffer,left[j%9]  ,p.x,p.y );
	}
     if (  pos2 ==1  )
    {
	draw_sprite(buffer,eright[h%3]  ,e.x,e.y );
	}
	if ( pos2 ==-1 )
    {
	draw_sprite(buffer,eleft[f%3]  ,e.x,e.y );
	}
    if (cxnxx==1) { cx=cx+5;cxnxxx=cxnxxx+5 ; draw_sprite(buffer,cart ,cx,cy );}
    if (cxnxx==-1) { cx=cx-5;cxnxxx=cxnxxx+5; draw_sprite(buffer,cart ,cx,cy ); }
	draw_sprite(buffer,vie[pourvie]  ,100,100 );

	if ( pourvie == 0 )
        {
            pourvie=10 ;
            nbdevie -- ;
        }

        draw_sprite(buffer,bonus  ,b1.x,b1.y );
//        draw_sprite(buffer,bonus  ,b2.x,b2.y );
//          draw_sprite(buffer,bonus  ,b3.x,b3.y );
        if ( nbdevie ==0 )
        {
         textprintf_centre_ex(buffer, font, 400,50,makecol(255, 0 ,0), -1,"dsl vous aver perdu");
        }
        textprintf_centre_ex(buffer, font, 750,50,makecol(255, 0 ,0), -1,"nbdevie : %d",nbdevie);


	blit(buffer,screen,0,0,0,0,1300,700);
}
int collision_background_right(perso p,BITMAP* backgroundcol)
{
	int x;
	int y;
	x=p.ix ;
	y=p.y  ;

	if( get_pixel(backgroundcol,x+100,y+10)== 255  || get_pixel(backgroundcol,x+100,y+90)== 255)
		return (1);

	return (0);
}

int collision_background_left(perso p,BITMAP* backgroundcol)
{
	int x;
	int y;
	x=p.ix ;
	y=p.y ;

	if( get_pixel(backgroundcol,x,y+10)== 255 ||  get_pixel(backgroundcol,x,y+90)== 255)
		return (1);

	return (0);
}
int collision_background_down(perso p,BITMAP* backgroundcol)
{

	int x;
	int y;
	x=p.ix ;
	y=p.y ;

	if( get_pixel(backgroundcol,x+50,y+100)== 255/* ||  get_pixel(backgroundcol,x+100,y+100)== 255*/)
		return (1);
	return (0);
}

int collision_background_up(perso p,BITMAP* backgroundcol)
{
	int x;
	int y;
	x=p.ix ;
	y=p.y ;

	if( get_pixel(backgroundcol,x,y)== 255 || get_pixel(backgroundcol,x+100,y)== 255)
		return (1);

	return (0);

}

int collision_background_ennem_right(ennem e,BITMAP* backgroundcol)
{
	int x;
	int y;
	x=e.x-bh ;
	y=e.y  ;

	if( get_pixel(backgroundcol,x+100,y+10)== 255  || get_pixel(backgroundcol,x+100,y+90)== 255)
		return (1);

	return (0);
}

int collision_background_ennem_left(ennem e,BITMAP* backgroundcol)
{
	int x;
	int y;
	x=e.x-bh ;
	y=e.y ;

	if( get_pixel(backgroundcol,x,y+10)== 255 ||  get_pixel(backgroundcol,x,y+90)== 255)
		return (1);

	return (0);
}

int get_pixel(BITMAP* buffer,int x,int y)
{
	int tmp;
	tmp=getpixel(buffer,x,y);
	return((getr(tmp) + getb(tmp) + getg(tmp)) / 3);
}
int collision(perso p, ennem e)
{
if ((p.x < e.x -300) || (p.y > e.y +300) ||(p.x > e.x+300 ) || (p.y < e.y-300 ))
{
return 0;
}
return 1;
}
int collisioncart(perso p, float cx , float cy)
{

if ((cx > p.x ) && (cy > p.y ) && (cx < p.x+100 ) && (cy  < p.y+100 ))
{
return 1;
}
return 0;
}


int collisionbonus (perso p, bon b1)
{
	float x;
	float y;
	x=b1.x ;
	y=b1.y ;
if ((x < p.x+100 ) && (p.x  <x+40) && (p.y<y+40 ) && (  p.y +100 >y ))
{
return 1;
}
return 0;
}





int collision_perso_ennem_right(perso p,ennem e)
{


	if(  p.x > e.x-100 && p.x -10 <e.x-100 && p.y+100>e.y  )
		return (1);

	return (0);
}

int collision_perso_ennem_left(perso p,ennem e)
{
	if(  p.x < e.x+100 && p.x +10 >e.x+100 && p.y+100>e.y )
		return (1);

	return (0);
}
int collision_perso_ennem_down(perso p,ennem e)
{

	if(  p.y > e.y-100  && p.x>e.x-100 &&p.x<e.x+100 )
		return (1);
	return (0);
}

int collision_perso_ennem_up(perso p,ennem e)
{
	if(  p.y < e.y && p.y +10 >e.x  )
		return (1);

	return (0);

}
